<?php

class OfficeHolderAction extends CommAction{
    //楼盘列表
    public function floorList(){
        //只有超级管理员和楼宇开发人员能看
        $w_id = $this->getUserId();
        $this->assign('w_id',$w_id);
        $mobile = M('login_redirect')->where(array('w_id'=>$w_id))->getField('account');
        $adminInfo = M('admin')->where(array('mobile'=>$mobile))->find();
        $roles = array(1,74,75);
        $this->assign('myrole',$adminInfo['role']);
        if(!in_array($adminInfo['role'], $roles)){
            // $map['id'] = 1000000000000;
            $this->assign('role','no');

        }
        // $map['status'] = 1;
        $info = M('loupan')->where($map)->order('createtime desc')->select();
        $this->assign('info',$info);
        $this->display();
    }


    //楼盘详情
    public function floorDetail(){

        $id = I('get.id');
        $info = M('loupan')->where('id='.$id)->find();
        $this->assign('info',$info);
        //计算楼盘总评分  楼盘总评分=楼盘基础评分x楼盘评分占比+楼盘活动评分x活动评分占比 
        $rate = M('point_rate')->where(array('id'=>1))->find();
        $point_sum = M('summary_lou')->where(array('l_id'=>$id))->sum('score');//多次活动总分数
        $count = M('summary_lou')->where(array('l_id'=>$id))->count();//多次活动总次数
        $avg_point = intval($point_sum/$count);
        $finish_point = intval(($info['score']*$rate['floor_rate'] + $avg_point*$rate['active_rate'])/100);
        $this->assign('finish_point',$finish_point);
        $this->display();
    }

    //楼盘列表新增
    public function addFloor(){

        if(IS_POST){
            $data['name'] = trim(I('name'));
            $activity = I('if-activity');
            if($activity == '是'){
                $data['activity'] = 1;
            }else{
                $data['activity'] = 2;
            }
            $data['price'] = trim(I('price'));
            $data['bank_num'] = trim(I('bank_num'));
            $data['alipay'] = trim(I('alipay'));
            $data['wx_num'] = trim(I('wx_num'));
            $data['jiaofeibeizhu'] = trim(I('beizhu'));
            $data['score'] = trim(I('score'));
            $data['shoufeipingzheng'] = I('shoufeipingzheng');
            $data['wuye'] = trim(I('wuye'));
            $data['wuyename'] = trim(I('wuyename'));
            $data['job'] = trim(I('job'));
            $data['mobile'] = trim(I('mobile'));
            $data['tel'] = trim(I('tel'));
            //$wuyefei = I('wuyefei');
            $data['wuyefee'] = I('wuyefei');
            $data['ruzhulv'] = I('ruzhulv');

            //$data['wuyefee'] = trim(I('wuyefei'));
            //$data['ruzhulv'] = trim(I('ruzhulv'));
            $address = explode(' ', I('address'));
            $data['province'] = $address[0];
            $data['city'] = $address[1];
            $data['district'] = $address[2];
            $data['add'] = trim(I('wyadd'));
            //$result=$this->turn_tengxun_location($data['province'].$data['city'].$data['district']);
            $data['lat']=I('latitude');
            $data['lng']=I('longitude');
            $data['guimo'] = I('workguimo');
            //$data['guimo'] = trim(I('workguimo'));
            $data['workernum'] = I('workernum');
            //$data['workernum'] = trim(I('workernum'));
            $data['cenggao'] = trim(I('cenggao'));
            $data['niandai'] = trim(I('niandai'));
            $data['park1'] = trim(I('park1'));
            $data['park2'] = trim(I('park2'));
            //上传外景图
            if (!empty($_FILES['logo1']['name'])) {  
                if ($_FILES["logo1"]["error"] > 0) {
                    $this->error('图片上传失败！');
                } else {
                    if (file_exists("./upload/loupan/" . $_FILES["logo1"]["name"])) {
                        echo $_FILES["logo1"]["name"] . " already exists. ";
                    } else {
                        $name2 = substr(strrchr($_FILES["logo1"]["name"], '.'), 1);
                        $name2 = date("YmdHis") . "_" . mt_rand() . "." . $name2;
                        $rs = move_uploaded_file($_FILES["logo1"]["tmp_name"],
                            "./upload/loupan/" . $name2);
                    }
                }
                $data['img1'] = $name2;
            }
            //入驻企业
            if (!empty($_FILES['logo2']['name'])) {  
                if ($_FILES["logo2"]["error"] > 0) {
                    $this->error('图片上传失败！');
                } else {
                    if (file_exists("./upload/loupan/" . $_FILES["logo2"]["name"])) {
                        echo $_FILES["logo2"]["name"] . " already exists. ";
                    } else {
                        $name2 = substr(strrchr($_FILES["logo2"]["name"], '.'), 1);
                        $name2 = date("YmdHis") . "_" . mt_rand() . "." . $name2;
                        $rs = move_uploaded_file($_FILES["logo2"]["tmp_name"],
                            "./upload/loupan/" . $name2);
                    }
                }
                $data['img2'] = $name2;
            }
            if (!empty($_FILES['logo22']['name'])) {  
                if ($_FILES["logo22"]["error"] > 0) {
                    $this->error('图片上传失败！');
                } else {
                    if (file_exists("./upload/loupan/" . $_FILES["logo22"]["name"])) {
                        echo $_FILES["logo22"]["name"] . " already exists. ";
                    } else {
                        $name2 = substr(strrchr($_FILES["logo22"]["name"], '.'), 1);
                        $name2 = date("YmdHis") . "_" . mt_rand() . "." . $name2;
                        $rs = move_uploaded_file($_FILES["logo22"]["tmp_name"],
                            "./upload/loupan/" . $name2);
                    }
                }
                $data['img22'] = $name2;
            }
            //$datingarea = I('datingarea');
            //if($datingarea == '50以下'){
            //    $data['datingarea'] = 1;
            //}
            //if($datingarea == '50-99'){
            //    $data['datingarea'] = 2;
            //}
            //if($datingarea == '100-199'){
            //    $data['datingarea'] = 3;
            //}
            //if($datingarea == '200以上'){
            //    $data['datingarea'] = 4;
            //}
            $data['datingarea'] = I('datingarea');
            //$changdiarea = I('changdiarea');
            //if($changdiarea == '5以下'){
            //    $data['changdiarea'] = 1;
            //}
            //if($changdiarea == '5-9.9'){
            //    $data['changdiarea'] = 2;
            //}
            //if($changdiarea == '10-19.9'){
            //    $data['changdiarea'] = 3;
            //}
            //if($changdiarea == '20以上'){
            //    $data['changdiarea'] = 4;
            //}
            $data['changdiarea'] = trim(I('changdiarea'));
            //$datingheight = I('datingheight');
            //if($datingheight == '5以下'){
            //    $data['datingheight'] = 1;
            //}
            //if($datingheight == '5-10'){
            //    $data['datingheight'] = 2;
            //}
            //if($datingheight == '10以上'){
            //    $data['datingheight'] = 3;
            //}
            $data['datingheight'] = trim(I('datingheight'));
            //if(I('kongtiao') == '有'){
            //    $data['kongtiao'] = 1;
            //}
            //if(I('kongtiao') == '无'){
            //    $data['kongtiao'] = 2;
            //}
            $data['kongtiao'] = I('kongtiao');
            //if(I('mainrukou') == '是'){
            //    $data['mainrukou'] = 1;
            //}
            //if(I('mainrukou') == '否'){
            //    $data['mainrukou'] = 0;
            //}
            $data['mainrukou'] = I('mainrukou');
            //if(I('dianyuan') == '有'){
            //    $data['dianyuan'] = 1;
            //}
            //if(I('dianyuan') == '无'){
             //   $data['dianyuan'] = 0;
            //}
            $data['dianyuan'] = trim(I('dianyuan'));
            $data['changdinum'] = trim(I('changdi-num'));
            $data['jinji'] = trim(I('jinji1'));
            //场地位置
            if (!empty($_FILES['logo3']['name'])) {  
                if ($_FILES["logo3"]["error"] > 0) {
                    $this->error('图片上传失败！');
                } else {
                    if (file_exists("./upload/loupan/" . $_FILES["logo3"]["name"])) {
                        echo $_FILES["logo3"]["name"] . " already exists. ";
                    } else {
                        $name2 = substr(strrchr($_FILES["logo3"]["name"], '.'), 1);
                        $name2 = date("YmdHis") . "_" . mt_rand() . "." . $name2;
                        $rs = move_uploaded_file($_FILES["logo3"]["tmp_name"],
                            "./upload/loupan/" . $name2);
                    }
                }
                $data['location'] = $name2;
            }
            if (!empty($_FILES['logo4']['name'])) {  
                if ($_FILES["logo4"]["error"] > 0) {
                    $this->error('图片上传失败！');
                } else {
                    if (file_exists("./upload/loupan/" . $_FILES["logo4"]["name"])) {
                        echo $_FILES["logo4"]["name"] . " already exists. ";
                    } else {
                        $name2 = substr(strrchr($_FILES["logo4"]["name"], '.'), 1);
                        $name2 = date("YmdHis") . "_" . mt_rand() . "." . $name2;
                        $rs = move_uploaded_file($_FILES["logo4"]["tmp_name"],
                            "./upload/loupan/" . $name2);
                    }
                }
                $data['location1'] = $name2;
            }
            $data['changdibeizhu'] = trim(I('changdi-beizhu'));
            $data['activitybeizhu'] = trim(I('activity-beizhu'));
            $data['peopletype'] = trim(I('peopletype'));
            $data['media'] = trim(I('media'));
            $data['createtime'] = time();
  // dump($data);die;          
            $res = M('loupan')->add($data);
            if($res){
                $this->simpleAlert('提交成功',U('floorList'));
            }else{
                $this->simpleAlert('请重新提交');
            }

        }
        $this->display();
    }

    //修改楼层
    public function edit_floor(){
        if(IS_POST){
            $data['name'] = trim(I('name'));
            $activity = I('if-activity');
            if($activity == '是'){
                $data['activity'] = 1;
            }else{
                $data['activity'] = 2;
            }
            $data['price'] = trim(I('price'));
            $data['bank_num'] = trim(I('bank_num'));
            $data['alipay'] = trim(I('alipay'));
            $data['wx_num'] = trim(I('wx_num'));
            $data['jiaofeibeizhu'] = trim(I('beizhu'));
            $data['score'] = trim(I('score'));
            $data['shoufeipingzheng'] = I('shoufeipingzheng');
            $data['wuye'] = trim(I('wuye'));
            $data['wuyename'] = trim(I('wuyename'));
            $data['job'] = trim(I('job'));
            $data['mobile'] = trim(I('mobile'));
            $data['tel'] = trim(I('tel'));
            //$wuyefei = I('wuyefei');
            $data['wuyefee'] = I('wuyefei');
            $data['ruzhulv'] = I('ruzhulv');

            //$data['wuyefee'] = trim(I('wuyefei'));
            //$data['ruzhulv'] = trim(I('ruzhulv'));
            $address = explode(' ', I('address'));
            $data['province'] = $address[0];
            $data['city'] = $address[1];
            $data['district'] = $address[2];
            $data['add'] = trim(I('wyadd'));
            //$result=$this->turn_tengxun_location($data['province'].$data['city'].$data['district']);
            $data['lat']=I('latitude');
            $data['lng']=I('longitude');
            $data['guimo'] = I('workguimo');
            //$data['guimo'] = trim(I('workguimo'));
            $data['workernum'] = I('workernum');
            //$data['workernum'] = trim(I('workernum'));
            $data['cenggao'] = trim(I('cenggao'));
            $data['niandai'] = trim(I('niandai'));
            $data['park1'] = trim(I('park1'));
            $data['park2'] = trim(I('park2'));
            //上传外景图
            if (!empty($_FILES['logo1']['name'])) {  
                if ($_FILES["logo1"]["error"] > 0) {
                    $this->error('图片上传失败！');
                } else {
                    if (file_exists("./upload/loupan/" . $_FILES["logo1"]["name"])) {
                        echo $_FILES["logo1"]["name"] . " already exists. ";
                    } else {
                        $name2 = substr(strrchr($_FILES["logo1"]["name"], '.'), 1);
                        $name2 = date("YmdHis") . "_" . mt_rand() . "." . $name2;
                        $rs = move_uploaded_file($_FILES["logo1"]["tmp_name"],
                            "./upload/loupan/" . $name2);
                    }
                }
                $data['img1'] = $name2;
            }
            //入驻企业
            if (!empty($_FILES['logo2']['name'])) {  
                if ($_FILES["logo2"]["error"] > 0) {
                    $this->error('图片上传失败！');
                } else {
                    if (file_exists("./upload/loupan/" . $_FILES["logo2"]["name"])) {
                        echo $_FILES["logo2"]["name"] . " already exists. ";
                    } else {
                        $name2 = substr(strrchr($_FILES["logo2"]["name"], '.'), 1);
                        $name2 = date("YmdHis") . "_" . mt_rand() . "." . $name2;
                        $rs = move_uploaded_file($_FILES["logo2"]["tmp_name"],
                            "./upload/loupan/" . $name2);
                    }
                }
                $data['img2'] = $name2;
            }
            if (!empty($_FILES['logo22']['name'])) {  
                if ($_FILES["logo22"]["error"] > 0) {
                    $this->error('图片上传失败！');
                } else {
                    if (file_exists("./upload/loupan/" . $_FILES["logo22"]["name"])) {
                        echo $_FILES["logo22"]["name"] . " already exists. ";
                    } else {
                        $name2 = substr(strrchr($_FILES["logo22"]["name"], '.'), 1);
                        $name2 = date("YmdHis") . "_" . mt_rand() . "." . $name2;
                        $rs = move_uploaded_file($_FILES["logo22"]["tmp_name"],
                            "./upload/loupan/" . $name2);
                    }
                }
                $data['img22'] = $name2;
            }
            //$datingarea = I('datingarea');
            //if($datingarea == '50以下'){
            //    $data['datingarea'] = 1;
            //}
            //if($datingarea == '50-99'){
            //    $data['datingarea'] = 2;
            //}
            //if($datingarea == '100-199'){
            //    $data['datingarea'] = 3;
            //}
            //if($datingarea == '200以上'){
            //    $data['datingarea'] = 4;
            //}
            $data['datingarea'] = I('datingarea');
            //$changdiarea = I('changdiarea');
            //if($changdiarea == '5以下'){
            //    $data['changdiarea'] = 1;
            //}
            //if($changdiarea == '5-9.9'){
            //    $data['changdiarea'] = 2;
            //}
            //if($changdiarea == '10-19.9'){
            //    $data['changdiarea'] = 3;
            //}
            //if($changdiarea == '20以上'){
            //    $data['changdiarea'] = 4;
            //}
            $data['changdiarea'] = trim(I('changdiarea'));
            //$datingheight = I('datingheight');
            //if($datingheight == '5以下'){
            //    $data['datingheight'] = 1;
            //}
            //if($datingheight == '5-10'){
            //    $data['datingheight'] = 2;
            //}
            //if($datingheight == '10以上'){
            //    $data['datingheight'] = 3;
            //}
            $data['datingheight'] = trim(I('datingheight'));
            //if(I('kongtiao') == '有'){
            //    $data['kongtiao'] = 1;
            //}
            //if(I('kongtiao') == '无'){
            //    $data['kongtiao'] = 2;
            //}
            $data['kongtiao'] = I('kongtiao');
            //if(I('mainrukou') == '是'){
            //    $data['mainrukou'] = 1;
            //}
            //if(I('mainrukou') == '否'){
            //    $data['mainrukou'] = 0;
            //}
            $data['mainrukou'] = I('mainrukou');
            //if(I('dianyuan') == '有'){
            //    $data['dianyuan'] = 1;
            //}
            //if(I('dianyuan') == '无'){
            //    $data['dianyuan'] = 0;
            //}
            $data['dianyuan'] = trim(I('dianyuan'));
            $data['changdinum'] = trim(I('changdi-num'));
            $data['jinji'] = trim(I('jinji1'));
            //场地位置
            if (!empty($_FILES['logo3']['name'])) {  
                if ($_FILES["logo3"]["error"] > 0) {
                    $this->error('图片上传失败！');
                } else {
                    if (file_exists("./upload/loupan/" . $_FILES["logo3"]["name"])) {
                        echo $_FILES["logo3"]["name"] . " already exists. ";
                    } else {
                        $name2 = substr(strrchr($_FILES["logo3"]["name"], '.'), 1);
                        $name2 = date("YmdHis") . "_" . mt_rand() . "." . $name2;
                        $rs = move_uploaded_file($_FILES["logo3"]["tmp_name"],
                            "./upload/loupan/" . $name2);
                    }
                }
                $data['location'] = $name2;
            }
            if (!empty($_FILES['logo4']['name'])) {  
                if ($_FILES["logo4"]["error"] > 0) {
                    $this->error('图片上传失败！');
                } else {
                    if (file_exists("./upload/loupan/" . $_FILES["logo4"]["name"])) {
                        echo $_FILES["logo4"]["name"] . " already exists. ";
                    } else {
                        $name2 = substr(strrchr($_FILES["logo4"]["name"], '.'), 1);
                        $name2 = date("YmdHis") . "_" . mt_rand() . "." . $name2;
                        $rs = move_uploaded_file($_FILES["logo4"]["tmp_name"],
                            "./upload/loupan/" . $name2);
                    }
                }
                $data['location1'] = $name2;
            }
            $data['changdibeizhu'] = trim(I('changdi-beizhu'));
            $data['activitybeizhu'] = trim(I('activity-beizhu'));
            $data['peopletype'] = trim(I('peopletype'));
            $data['media'] = trim(I('media'));
            $data['createtime'] = time();
            $data['status'] = '';
            $id = $_POST['l_id'];

            //日志原数据
            $w_id = $this->getUserId();
            $this->assign('w_id',$w_id);
            $mobile = M('login_redirect')->where(array('w_id'=>$w_id))->getField('account');
            $log['admin'] = M('admin')->where(array('mobile'=>$mobile))->getField('truename');
            $log['l_id'] = $_POST['l_id'];
            $log['l_name'] = $_POST['l_name'];
            $log['l_activity'] = $_POST['l_activity'];
            $log['new_activity'] = trim(I('if-activity'));
            $log['l_score'] = $_POST['l_score'];
            $log['new_score'] = trim(I('score'));
            $log['l_price'] = $_POST['l_price'];
            $log['new_price'] = trim(I('price'));
            $log['l_jiaofeibeizhu'] = $_POST['l_jiaofeibeizhu'];
            $log['new_jiaofeibeizhu'] = trim(I('beizhu'));
            $log['l_mobile'] = $_POST['l_mobile'];
            $log['new_mobile'] = trim(I('mobile'));
            $log['l_changdibeizhu'] = $_POST['l_changdibeizhu'];
            $log['new_changdibeizhu'] = trim(I('changdi-beizhu'));
            $log['l_activitybeizhu'] = $_POST['l_activitybeizhu'];
            $log['new_activitybeizhu'] = trim(I('activity-beizhu'));
            $log['l_peopletype'] = $_POST['l_peopletype'];
            $log['new_peopletype'] = trim(I('peopletype'));
            $log['l_media'] = $_POST['l_media'];
            $log['new_media'] = trim(I('media'));
            $log['create_time'] = time();
            M('loupan_log')->add($log);

            $res = M('loupan')->where('id='.$id)->save($data);
            if($res){
                $this->simpleAlert('修改成功',U('floorList'));
            }else{
                $this->simpleAlert('请重新提交');
            }

        }
        $id = $_GET['id'];
        $info = M('loupan')->where(array('id'=>$id))->find();
        $this->assign('info',$info);
        $this->display();
    }
    //运输列表
    public function transportList(){
       //除了管理员和审核员,roleid分别为1,2的,其余人只能看到跟自己有关的信息
       $w_id = $this->getUserId();
       $mobile = M('login_redirect')->where(array('w_id'=>$w_id))->getField('account');
       $adminInfo = M('admin')->where(array('mobile'=>$mobile))->find();
       $roles = array(1,67,69,70,76);
       $this->assign('myrole',$adminInfo['role']);
       if(in_array($adminInfo['role'], $roles)){
           if($adminInfo['role'] == 67){
               $map['ac.d_id'] = $adminInfo['id'];
               // $map['ac.create_time'] = array('gt',date('Y-m-d',time()-1));
           }
           if($adminInfo['role'] == 69){
               $map['ac.dudao'] = $adminInfo['truename'];
               // $map['ac.create_time'] = array('gt',date('Y-m-d',time()-1));
           }
           if($adminInfo['role'] == 70){
               $map['ac.principal'] = $adminInfo['truename'];
               // $map['ac.create_time'] = array('gt',date('Y-m-d',time()-1));
           }
       }else{
               // $map['ac.id'] = 1000000000000;
               $this->assign('role','no');
           }
        $map['ac.status'] = 1;
        $map['l.status'] = 1;
        // 当天的零点
        $dayBegin = strtotime(date('Y-m-d', time())); 
        // 当天的24
        $dayEnd = $dayBegin + 2 * 24 * 60 * 60 - 1;
        $map['ac.create_time'] = array('between',array($dayBegin,$dayEnd));
       
        $info = M('active as ac')
              ->join("jl_admin as a on a.id=ac.d_id")
              ->join("jl_loupan as l on l.id=ac.name")
              ->field('ac.id as actid,a.truename as driver,ac.aname as program,ac.finish,l.name as loupan,ac.dudao,ac.principal')
              ->where($map)
              ->order('ac.create_time')
              ->select();
        $this->assign('info',$info);
        
        $this->display();
    }

    //运输列表详情
    public function transportDetail(){

        
        if(IS_POST){
            // dump($_POST);die;
            $w_id = $this->getUserId();
            $mobile = M('login_redirect')->where(array('w_id'=>$w_id))->getField('account');
            $adminInfo = M('admin')->where(array('mobile'=>$mobile))->find();
            $roles = array(1,67,69,70,76);
            if($adminInfo['role'] == 67){
                $actid =   I('post.actid');
                if (!empty($_FILES['come_img']['name'])) {  
                    if ($_FILES["come_img"]["error"] > 0) {
                        $this->error('图片上传失败！');
                    } else {
                        if (file_exists("./upload/loupan/" . $_FILES["come_img"]["name"])) {
                            echo $_FILES["come_img"]["name"] . " already exists. ";
                        } else {
                            $name2 = substr(strrchr($_FILES["come_img"]["name"], '.'), 1);
                            $name2 = date("YmdHis") . "_" . mt_rand() . "." . $name2;
                            $rs = move_uploaded_file($_FILES["come_img"]["tmp_name"],
                                "./upload/loupan/" . $name2);
                        }
                    }
                    $data['come_img'] = $name2;
                }
                //入驻企业
                if (!empty($_FILES['leave_img']['name'])) {  
                    if ($_FILES["leave_img"]["error"] > 0) {
                        $this->error('图片上传失败！');
                    } else {
                        if (file_exists("./upload/loupan/" . $_FILES["leave_img"]["name"])) {
                            echo $_FILES["leave_img"]["name"] . " already exists. ";
                        } else {
                            $name2 = substr(strrchr($_FILES["leave_img"]["name"], '.'), 1);
                            $name2 = date("YmdHis") . "_" . mt_rand() . "." . $name2;
                            $rs = move_uploaded_file($_FILES["leave_img"]["tmp_name"],
                                "./upload/loupan/" . $name2);
                        }
                    }
                    $data['leave_img'] = $name2;
                }
                $save = M('active')->where(array('id'=>$actid))->save($data);
                //检查是否需要更改为已完成的状态
                $checkFinish = M('active')->where(array('id'=>$actid))->find();
                if(!empty($checkFinish['come_img'])  && !empty($checkFinish['leave_img'])){
                    $map['finish'] = 1;
                    M('active')->where(array('id'=>$actid))->save($map);
                }
                if($save){
                    $this->simpleAlert('提交成功',U('transportList'));
                }else{
                    $this->simpleAlert('请重新提交');
                }
            }else{
                $this->simpleAlert('您不是运输人员,无法进行此操作哦!');
            }
            
            
            
        }
        $actid = I('get.actid');
        $info = M('active as ac')
              ->join("jl_admin as a on a.id=ac.d_id")
              ->join("jl_loupan as l on l.id=ac.name")
              ->field('ac.id as actid,a.truename as driver,ac.aname as program,ac.come_img,ac.leave_img,ac.finish,l.name as loupan,ac.summary,ac.create_time')
              ->where('ac.id="'.$actid.'"')
              ->order('ac.create_time')
              ->find();
        $this->assign('info',$info);
        //撤场照片与兼职岗位结束时间同步
        $act_name = M('active')->where(array('id'=>$actid))->find();
        $parttime_info = M('parttime_job')->where(array('job_name'=>$act_name['aname']))->find();
        $leave = strtotime(date('Y-m-d',$act_name['create_time']).' '.$parttime_info['end_hour']);
        // $leave = $info['create_time'] + 32400;
        if($leave < time()){
            $leave = 1;//可撤场
        }else{
            $leave = 2;
        }
        $this->assign('leave',$leave);
        $this->display();
    }


    //活动详情列表
    public function activityList(){
        //除了管理员和审核员,roleid分别为1,2的,其余人只能看到跟自己有关的信息
        $w_id = $this->getUserId();
        $this->assign('w_id',$w_id);
        $mobile = M('login_redirect')->where(array('w_id'=>$w_id))->getField('account');
        $adminInfo = M('admin')->where(array('mobile'=>$mobile))->find();
        $roles = array(1,69,70,76);
        
        if(in_array($adminInfo['role'], $roles)){
            $role1 = array(1,70);
            if(in_array($adminInfo['role'], $role1)){
                $this->assign('myrole',1);
            }else{
                $this->assign('myrole',2);
            }
            if($adminInfo['role'] == 70){
            $map['ac.principal'] = $adminInfo['truename'];
            }else if($adminInfo['role'] == 69){
                $map['ac.dudao'] = $adminInfo['truename'];
            }
        }else{
                // $map['ac.id'] = 1000000000000;
                $this->assign('role','no');
        }
        
        
        $map['l.status'] = 1;
        $map['ac.status'] = 1;
        // 当天的零点
        $dayBegin = strtotime(date('Y-m-d', time())); 
        // 当天的24
        $dayEnd = $dayBegin + 24 * 60 * 60 - 1;
        $map['ac.create_time'] = array('between',array($dayBegin,$dayEnd));
        $info = M('active as ac')
              ->join('jl_loupan as l on l.id=ac.name')
              ->field('ac.id as actid,ac.addr as city,ac.principal,ac.aname,l.name')
              ->where($map)
              ->order('ac.create_time desc')
              ->select();
        $this->assign('info',$info);
        $this->display();
    }

    //活动详情
    public function activityDetail(){
     
        $actid = I('get.actid');
        $info = M('active as ac')
              ->join('jl_loupan as l on l.id=ac.name')
              ->join('jl_admin as ad on ad.id=ac.d_id')
              ->field('ac.aname as program,ac.principal,ac.addr as city,l.name as loupan,ac.create_time,ac.dudao,ad.truename as driver')
              ->where('ac.id="'.$actid.'"')
              ->find();
        $this->assign('info',$info);
        //获取促销生信息
        $studentname = M('active')->where(array('id'=>$actid))->find();
        $map['w_id']=array('in',json_decode($studentname['w_id']));
        $stname = M('student')->where($map)->select();
        $this->assign('stname',$stname);
        //获取绩效数据
        $where['id'] = array('in',json_decode($studentname['o_id']));
        $jixiao = M('active_jixiao')->where($where)->select();
        $this->assign('jixiao',$jixiao);

        //签到信息
        $w_ids = json_decode($studentname['w_id']);
        foreach($w_ids as $k=>$val){
            $all[$k]['s_name'] = M('student')->where(array('w_id'=>$val))->getField('name');
            $all[$k]['qiandao'] = M('sales_join')->where(array('a_id'=>$actid,'w_id'=>$val))->getField('qd_status');
            $all[$k]['qd_time'] = M('sales_join')->where(array('a_id'=>$actid,'w_id'=>$val))->getField('qd_time');
            $all[$k]['start_img'] = json_decode(M('sales_join')->where(array('a_id'=>$actid,'w_id'=>$val))->getField('start_img'));
            $all[$k]['end_img'] = json_decode(M('sales_join')->where(array('a_id'=>$actid,'w_id'=>$val))->getField('end_img'));
            //绩效
            // foreach(json_decode($studentname['o_id']) as $k1=>$val1){
            $all[$k]['paifa'] = M('record')->where(array('a_id'=>$actid,'w_id'=>$val,'o_type'=>1))->sum('num');
            $all[$k]['goumai'] = M('record')->where(array('a_id'=>$actid,'w_id'=>$val,'o_type'=>2))->sum('num');
            $all[$k]['guanzhu'] = M('record')->where(array('a_id'=>$actid,'w_id'=>$val,'o_type'=>3))->sum('num');
            // }
        }
        // dump($all);
        $this->assign('all',$all);
        $this->display();
    }

    //新增活动
    public function addActivity(){
        //楼盘名称
        $programName = M('loupan')->where(array('status'=>1))->select();
        $this->assign('programName',$programName);
        //促销生
        $sales = M('student')->where(array('status'=>0))->select();
        $this->assign('sales',$sales);
        //司机
        $drivers = M('admin')->where("role=67 and status=0")->select();
        $this->assign('drivers',$drivers);
        //绩效
        $jixiao = M('active_jixiao')->select();
        $this->assign('jixiao',$jixiao);
        //负责人
        $principal = M('admin')->where(array('role'=>70))->getField('truename',true);
        $this->assign('principal',$principal);
        //督导
        $dudao = M('admin')->where(array('role'=>69))->getField('truename',true);
        $this->assign('dudao',$dudao);
        if(IS_POST){
        
            $data['aname'] = trim(I('post.aname'));
            $data['principal'] = trim(I('post.principal'));
            $address = explode(' ', I('city'));
            //$data['province'] = $address[0];
            //$data['city'] = $address[1];
            //$data['district'] = $address[2];
            $data['addr'] = $address[1];
            $data['name'] = M('loupan')->where(array('name'=>I('post.loupan')))->getField('id');
            $data['create_time'] = strtotime(I('post.create_time'));
            $data['dudao'] = trim(I('post.dudao'));
            $sales_name = explode(',',$_POST['sales']);
       
            foreach($sales_name as $k=>$val){
                $w_id[] = M('student')->where(array('name'=>$val))->getField('w_id');
                
            }
            $data['w_id'] = json_encode($w_id);
            $data['d_id'] = M('admin')->where(array('truename'=>I('post.driver')))->getField('id');
            $data['o_id'] = json_encode(M('active_jixiao')->where(array('name'=>I('post.jixiao')))->getField('id',true));
        
            M('active')->add($data);
            $this->simpleAlert('提交成功',U('activityList'));
        }
        $this->display();
    }


    //总结列表
    public function summaryList(){
        //除了管理员和审核员,roleid分别为1,2的,其余人只能看到跟自己有关的信息
        $w_id = $this->getUserId();
        $this->assign('w_id',$w_id);
        $mobile = M('login_redirect')->where(array('w_id'=>$w_id))->getField('account');
        $adminInfo = M('admin')->where(array('mobile'=>$mobile))->find();
        $roles = array(1,2,69,70,76);
        if(!in_array($adminInfo['role'], $roles)){
            $this->assign('role','no');
        }else{
            if($adminInfo['role'] == 70){
                $map['ac.principal'] = $adminInfo['truename'];
            }
            if($adminInfo['role'] == 69){
               $map['ac.dudao'] = $adminInfo['truename'];
           }
        }
        $map['ac.status'] = 1;
        // 当天的零点
        $dayBegin = strtotime(date('Y-m-d', time())); 
        // 当天的24
        $dayEnd = $dayBegin + 24 * 60 * 60 - 1;
        // $map['ac.create_time'] = array('between',array($dayBegin,$dayEnd));
        $info = M('active as ac')
              ->join('jl_loupan as l on l.id=ac.name')
              ->join('jl_admin as ad on ad.truename=ac.principal')
              ->join('jl_student as st on st.w_id=ac.w_id')
              ->field('ac.id,ac.aname as name,st.name as student_name,l.name as loupan,ac.addr as city,ac.principal,ac.dudao,ac.status,ac.create_time,ac.summary')
              ->order('ac.create_time desc')
              ->where($map)
              ->select();
        // dump(M()->getLastSql());dump($info);
        $this->assign('info',$info);
        $this->display();
    }

    //总结详情
    public function summaryDetail(){
        if(IS_POST){
            //var_dump($_POST);
            $l_data['l_id']=$_POST['l_id'];
            $l_data['a_id']=$_POST['actid'];
            $l_data['summaryinput_flowrate'] = $_POST['summaryinput_flowrate'];
            $l_data['summaryinput_involvement'] = $_POST['summaryinput_involvement'];
            $l_data['summaryinput_adaptability'] = $_POST['summaryinput_adaptability'];
            $l_data['score'] = $_POST['l_score'];
            $l_data['describe'] = $_POST['desc'];
            $l_data['create_time'] = time();
            $l=M("summary_lou")->add($l_data);
            //if($l){
            //    $res=array("res"=>1,"msg"=>"评论成功");
            //}else{
            //    $res=array("res"=>2,"msg"=>"评论失败");
            //}
            $d_data['d_id'] = $_POST['d_id'];
            $d_data['a_id'] = $_POST['actid'];
            $d_data['desc'] =  implode(",",$_POST['d_desc']);
            //$d_data['desc'] =  $_POST['driver-pinglun'];
            $d_data['score'] = $_POST['d_score'];
            $d_data['create_time'] = time();
             $d=M("summary_dir")->add($d_data);
            //if($d){
            //    $res=array("res"=>1,"msg"=>"评论成功");
            //}else{
            //    $res=array("res"=>2,"msg"=>"评论失败");
            //}
            foreach($_POST['student'] as $vo){
                $s_data['w_id'] = $vo['w_id'];
                $s_data['a_id'] = $_POST['actid'];
                $s_data['desc'] = implode(",",$vo['s_desc']);
                $s_data['score'] = $vo['score'];
                $s_data['create_time'] = time();
                //$s_exist = M('summary_stu')->where(array('w_id'=>$s_data['w_id'],'a_id'=>$s_data['a_id']))->find();
                
                $s=M('summary_stu')->add($s_data);
                //if($s){   
                //    $res=array("res"=>1,"msg"=>"评论成功");
                //}else{
                //    $res=array("res"=>2,"msg"=>"评论失败");
                //}
            }
                
                if($l && $d && $s){
                    $ss['summary'] = 1;
                    M('active')->where(array('id'=>$_POST['actid']))->save($ss);
                    $res=array("res"=>1,"msg"=>"评论成功");
                }else{
                    $res=array("res"=>2,"msg"=>"评论失败");
                }
                echo json_encode($res);

        }else{
                $actid = $_GET['actid'];
                $summary = $_GET['summary'];

                $this->assign('actid',$actid);
                $this->assign('summary',$summary);
                //项目信息
                $info = M('active as ac')
                      ->join('jl_loupan as l on l.id=ac.name')
                      ->field('ac.id,ac.d_id,ac.aname as program,l.id as l_id,l.name as loupan,ac.addr as city,ac.summary')
                      ->where('ac.id='.$actid)
                      ->find();

                $this->assign('info',$info);
                //促销生信息
                $s_list=M("active")->where("id=".$actid)->find();
                $s_map['s.w_id']=array("in",json_decode($s_list['w_id'],true));
                $student_info = M('student as s')
                    ->where($s_map)->select();
                $s_map['ss.a_id']=$actid;
                $student = M('student as s')
                    ->join("jl_summary_stu as ss on ss.w_id=s.w_id")
                    ->where($s_map)->select();
                $this->assign('student',$student);
                //总结的信息
                $summaryInfo = M('summary')->where(array('a_id'=>$_GET['actid']))->find();
                $this->assign('summaryInfo',$summaryInfo);
                $this->assign('studentinfo',$student_info);
                //详情显示的数据
                $summary_loupan = M('summary_lou')->where('a_id='.$actid)->find();
                $summary_dir = M('summary_dir')->where('a_id='.$actid)->find();
                $summary_stu = M('summary_stu')->where('a_id='.$actid)->select();

                //$driver_pingjia = implode(',',json_decode($summary_dir['desc']));
                //var_dump($driver_pingjia);
                $this->assign('summary_loupan',$summary_loupan);
                $this->assign('summary_dir',$summary_dir);
                //$this->assign('driver_pingjia',$driver_pingjia);
                $this->assign('summary_stu',$summary_stu);
                $this->display();
                
                
            }
    }


    //直播列表
    public function video_list(){
        //除了管理员和审核员,roleid分别为1,2的,其余人都无法看到
        $w_id = $this->getUserId();
        $this->assign('w_id',$w_id);
        $mobile = M('login_redirect')->where(array('w_id'=>$w_id))->getField('account');
        $adminInfo = M('admin')->where(array('mobile'=>$mobile))->find();
        $roles = array(1,2,69,70,76);
        if(!in_array($adminInfo['role'], $roles)){
            // $map['ac.id'] = 10000000000000000;
            $this->assign('role','no');
            
        }else{
            if($adminInfo['role'] == 69){
                $map['ac.dudao'] = $adminInfo['truename'];
            }
            if($adminInfo['role'] == 70){
                $map['ac.principal'] = $adminInfo['truename'];
            }
        }
        // $map['ac.create_time'] = array('gt',time());
        // 当天的零点
        $dayBegin = strtotime(date('Y-m-d', time())); 
        // 当天的24
        $dayEnd = $dayBegin + 24 * 60 * 60 - 1;
        $map['v.a_time'] = array('between',array($dayBegin,$dayEnd));
// $map['v.a_time'] = array('egt',$dayBegin);
        $info = M('video as v')
              ->join('jl_active as ac on ac.aname=v.a_id')
              ->join('jl_loupan as l on l.id=v.l_id')
              ->field('ac.addr,ac.aname,l.name,ac.dudao,v.id,v.url')
              ->where($map)
              ->order('v.createtime')
              ->group('v.l_id')
              ->select();
    // var_dump($info);var_dump(M()->getLastSQL());
        $this->assign('info',$info);
        //搜索信息
        $citys = M('video as v')
              ->join('jl_active as ac on ac.id=v.a_id')
              ->join('jl_loupan as l on l.id=ac.name')
              ->field('ac.addr')
              ->group('ac.addr')
              ->select();
        $program = M('video as v')
              ->join('jl_active as ac on ac.id=v.a_id')
              ->join('jl_loupan as l on l.id=ac.name')
              ->field('ac.aname')
              ->group('ac.aname')
              ->select();
        $dudao = M('video as v')
              ->join('jl_active as ac on ac.id=v.a_id')
              ->join('jl_loupan as l on l.id=ac.name')
              ->field('ac.dudao')
              ->group('ac.dudao')
              ->select();
        $loupan = M('video as v')
              ->join('jl_active as ac on ac.id=v.a_id')
              ->join('jl_loupan as l on l.id=ac.name')
              ->field('l.name as loupan')
              ->group('l.name')
              ->select();
        $this->assign('citys',$citys);
        $this->assign('program',$program);
        $this->assign('dudao',$dudao);
        $this->assign('loupan',$loupan);
        $this->display();
    }

    //新增直播
    public function add_video(){
        $info = M('camera')->select();
        $this->assign('info',$info);
        $this->display();
    }
    // public function lat_lng($addr){
    //     $url="http://api.map.baidu.com/geocoder/v2/?address=".$addr."&output=json&ak=p745ACU3lAEAa1zLzH6UGufeegdcXLix&callback=showLocation";
    //     $ch = curl_init();
    //     curl_setopt($ch, CURLOPT_URL, $url);
    //     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //     curl_setopt($ch, CURLOPT_HEADER, 0);
    //     $output = curl_exec($ch);
    //     curl_close($ch);
    //     $a=explode("(",$output);
    //     $b=explode(")",$a[1]);
    //     $result=json_decode($b[0],true);
    //     return $result['result']['location'];
    // }
    //测试根据地址转换成腾讯地图的经纬度
    public function turn_tengxun_location($addr="江苏省南京市雨花台区软件大道106号"){
        $url = "http://apis.map.qq.com/ws/geocoder/v1/?address=".$addr."&key=4WIBZ-IIB3J-JBIF3-FSHJ4-THHCQ-WKFVO&   output=json&callback=function1";
        $res = $this->httpGet($url);
        return $res;
    }

    public function httpGet($url){
        $ch = curl_init();
        //设置选项，包括URL
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        //执行并获取HTML文档内容
        $output = curl_exec($ch);
        //释放curl句柄
        curl_close($ch);
        return json_decode($output,true)['result']['location'];
    }

    public function map(){
        $this->display();
    }
}
?>